home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 13 - 1997 (partial) / 13.02 Feb 97 / SerialKiller SN Generator / Tester.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-22  |  5.5 KB  |  223 lines  |  [TEXT/CWIE]

  1. //////////////////////////////////////////////////
  2. //                                                //
  3. //    Tester.c    for SerialTester                //
  4. //                                                //
  5. //    © 1996 1 A.M. Productions and J. McCornack    //
  6. //    September 9,1996                            //
  7. //                                                //
  8. //    This demonstrates serial encoding based     //
  9. //    on simple mathmatical functions performed     //
  10. //    on one seed number split in half. Both         //
  11. //    encoding processes are the same for the        //
  12. //    sake of demonstration.                        //
  13. //                                                //
  14. //    Tests for serial numbers created by            //
  15. //    SerialGenerator.                            //
  16. //                                                //
  17. //////////////////////////////////////////////////
  18.  
  19. #ifndef __MAIN__
  20. #include "Tester.h"
  21. #endif
  22.  
  23. void main()
  24. {
  25.     InitGraf(&qd.thePort);
  26.     InitFonts();
  27.     FlushEvents(everyEvent,0);
  28.     InitWindows();
  29.     InitMenus();
  30.     TEInit();
  31.     InitDialogs(nil);        
  32.         
  33.     GetNumber();        // Ask the user for serial numbers to test
  34.  
  35.     FlushEvents(everyEvent,0);        
  36. }
  37.  
  38. void GetNumber()
  39. {
  40.     short         itemHit, itemType, i;
  41.     Handle         itemHandle;
  42.     Rect         itemRect;
  43.     Str255        number;    
  44.     DialogPtr     dialog;
  45.     GrafPtr     oldPort;
  46.  
  47.     GetPort(&oldPort);
  48.     dialog=GetNewDialog(129,nil,(WindowPtr)-1);    //129 is the Tester dialog
  49.     ShowWindow(dialog);
  50.     SelectWindow(dialog);
  51.     SetPort(dialog);
  52.     GetDialogItem(dialog,3,&itemType,&itemHandle,&itemRect);
  53.     SetDialogItemText(itemHandle,"\p000-930-526-002");       //Start with a good one
  54.     
  55.     itemHit=-1;
  56.  
  57.     while (itemHit !=2)
  58.     {
  59.         ModalDialog(StdFilter, &itemHit);
  60.         
  61.         if (itemHit == 2)
  62.         {
  63.             SetPort(oldPort);
  64.             DisposeDialog(dialog);
  65.             ExitToShell();
  66.         }
  67.         if (itemHit == 1)
  68.         {
  69.             for (i=1;i <=15; i++)
  70.                 number[i] = '0';
  71.             GetDialogItem(dialog,3,&itemType,&itemHandle,&itemRect);
  72.             GetDialogItemText(itemHandle,number);
  73.             if (CheckValue(number))
  74.             {
  75.                 GetDialogItem(dialog,4,&itemType,&itemHandle,&itemRect);
  76.                 SetDialogItemText(itemHandle,"\p    Good!");
  77.                 SysBeep(1);
  78.             }
  79.             else
  80.             {
  81.                 GetDialogItem(dialog,4,&itemType,&itemHandle,&itemRect);
  82.                 SetDialogItemText(itemHandle,"\p   No good.");
  83.             }
  84.         
  85.         }
  86.     }
  87. }
  88.  
  89. pascal Boolean StdFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
  90. {
  91.     char theChar;
  92.     short itemKind;
  93.     Handle itemHandle;
  94.     Rect itemBox;
  95.  
  96.     switch ( theEvent->what )
  97.     {
  98.     case keyDown: 
  99.         theChar = (char)(theEvent->message & charCodeMask);
  100.         if ( (((theEvent->modifiers & cmdKey) != 0) && (theChar == '.')) || (theChar == (char)27) ) /*cmd-. or ESC*/
  101.         {
  102.             *itemHit = kCancelButton;
  103.             GetDItem(theDialog, kCancelButton, &itemKind, &itemHandle, &itemBox);
  104.             HiliteControl((ControlHandle)itemHandle, 1);
  105.             return true;
  106.         }
  107.         if ( (theChar == (char)13) || (theChar == (char)3) )
  108.         {
  109.             *itemHit = kOKButton;
  110.             GetDItem(theDialog, 1, &itemKind, &itemHandle, &itemBox);
  111.             HiliteControl((ControlHandle)itemHandle, kOKButton);
  112.             return true;
  113.         }
  114.         break;
  115.     case updateEvt:
  116.         BeginUpdate(theDialog);
  117.         SetPort(theDialog);
  118.         DrawDialog(theDialog);
  119.         GetDItem(theDialog, kOKButton, &itemKind, &itemHandle, &itemBox);
  120.         InsetRect(&itemBox, -4, -4);
  121.         PenSize(3, 3);
  122.         FrameRoundRect(&itemBox, 15, 15);
  123.         EndUpdate(theDialog);
  124.         break;
  125.     }
  126.     return false;
  127. }
  128.  
  129. Boolean CheckValue(Str255 valStr)
  130. {
  131.     long    i,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12;
  132.     long    val1,val2,val3,val4;
  133.     long    temp1, temp2;
  134.     
  135.     i=1;
  136.     // Parse through string and pull out digits.
  137.     // This allows you to encode your serial numbers further
  138.     // by adding letters and symbols - A154TG234453-H452
  139.     
  140.     while ((valStr[i] < '0') || (valStr[i] > '9')) i++;
  141.     d1 = valStr[i++] - '0';
  142.     while ((valStr[i] < '0') || (valStr[i] > '9')) i++;
  143.     d2 = valStr[i++] - '0';
  144.     while ((valStr[i] < '0') || (valStr[i] > '9')) i++;
  145.     d3 = valStr[i++] - '0';
  146.     while ((valStr[i] < '0') || (valStr[i] > '9')) i++;
  147.     d4 = valStr[i++] - '0';
  148.     while ((valStr[i] < '0') || (valStr[i] > '9')) i++;
  149.     d5 = valStr[i++] - '0';
  150.     while ((valStr[i] < '0') || (valStr[i] > '9')) i++;
  151.     d6 = valStr[i++] - '0';
  152.     while ((valStr[i] < '0') || (valStr[i] > '9')) i++;
  153.     d7 = valStr[i++] - '0';
  154.     while ((valStr[i] < '0') || (valStr[i] > '9')) i++;
  155.     d8 = valStr[i++] - '0';
  156.     while ((valStr[i] < '0') || (valStr[i] > '9')) i++;
  157.     d9 = valStr[i++] - '0';
  158.     while ((valStr[i] < '0') || (valStr[i] > '9')) i++;
  159.     d10 = valStr[i++] - '0';
  160.     while ((valStr[i] < '0') || (valStr[i] > '9')) i++;
  161.     d11 = valStr[i++] - '0';
  162.     while ((valStr[i] < '0') || (valStr[i] > '9')) i++;
  163.     d12 = valStr[i++] - '0';
  164.     
  165.     d1 = (d1*100);
  166.     d2 = (d2*10);
  167.     
  168.     temp1 = d1+d2+d3;
  169.     
  170.     d2 = d2 + 10;
  171.     d3 = d3 + 3;
  172.     d2 = d2 + ((d3/10)*10);
  173.     d1 = d1 + ((d2/100)*100);
  174.     d1 = d1-((d1/1000)*1000);
  175.     d2 = d2-((d2/100)*100);
  176.     d3 = d3-((d3/10)*10);                // Add 13    
  177.     
  178.     val1 = (d3*100)+(d2)+(d1/100);        // Stir Numbers
  179.     val1 = val1 * 3;                    // Multiply by 3
  180.     val1 = val1 - (temp1*2);            // Subtract the original * 2
  181.     if (val1<0)
  182.         val1 = -val1;                    // Adjust if necessary
  183.     d3 = (val1 % 10);                    // Reassign values
  184.     d2 = ((val1 % 100)-d3)/10;
  185.     d1 = ((val1 % 1000)-((d2*10)+d3))/100;
  186.     
  187.     val1 = (d1*100)+(d2*10)+d3;
  188.     val2 = (d4*100)+(d5*10)+d6;
  189.     
  190.     // Second Number Set
  191.     
  192.     d10 = (d10*100);
  193.     d11 = (d11*10);
  194.     
  195.     temp2 = d10+d11+d12;
  196.     
  197.     d11 = d11 + 10;
  198.     d12 = d12 + 3;
  199.     d11 = d11 + ((d12/10)*10);
  200.     d10 = d10 + ((d11/100)*100);
  201.     d10 = d10-((d10/1000)*1000);
  202.     d11 = d11-((d11/100)*100);
  203.     d12 = d12-((d12/10)*10);            // Add 13
  204.     
  205.     val4 = (d12*100)+(d11)+(d10/100);    // Stir Numbers
  206.     val4 = val4 * 3;                    // Multiply by 3
  207.     val4 = val4 - (temp2*2);            // Subtract the original * 2
  208.     if (val4<0)
  209.         val4 = -val4;                    // Adjust if necessary
  210.     d12 = (val4 % 10);                    // Reassign values
  211.     d11 = ((val4 % 100)-d12)/10;
  212.     d10 = ((val4 % 1000)-((d11*10)+d12))/100;
  213.     
  214.     val4 = (d10*100)+(d11*10)+d12;
  215.     val3 = (d7*100)+(d8*10)+d9;
  216.     
  217.     if ((val1 == val2) && (val3 == val4))
  218.         return (true);
  219.     else
  220.         return (false);
  221.  
  222. }
  223.